package com.gduf.lsm.service;


import cn.hutool.core.util.ObjectUtil;
import com.gduf.lsm.common.enums.ResultCodeEnum;
import com.gduf.lsm.entity.Notice;

import com.gduf.lsm.exception.CustomException;
import com.gduf.lsm.mapper.NoticeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公告信息表业务处理
 **/
@Service
public class NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 新增
     * @param notice
     */
    public void add(Notice notice) {
        //判断公告标题、内容和通知对象不为空才可以插入
        if(ObjectUtil.isEmpty(notice.getTitle())||ObjectUtil.isEmpty(notice.getContent())||ObjectUtil.isEmpty(notice.getChara())){
            throw new CustomException(ResultCodeEnum.NOTICE_MAIN_NULL_ERROR);
        }
        noticeMapper.insert(notice);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(Integer id) {
        noticeMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            noticeMapper.deleteById(id);
        }
    }

    /**
     * 修改
     * @param notice
     */
    public void updateById(Notice notice) {
        noticeMapper.updateById(notice);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Notice selectById(Integer id) {
        return noticeMapper.selectById(id);
    }

    /**
     * 查询所有
     * @param notice
     * @return
     */
    public List<Notice> selectAll(Notice notice) {
        return noticeMapper.selectAll(notice);
    }

    /**
     * 分页查询
     * @param notice
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<Notice> selectPage(Notice notice, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Notice> list = noticeMapper.selectAll(notice);
        return PageInfo.of(list);
    }

}